#include "main.h"
#include "parameters.h"

#ifdef DEBUG
void usage(char *str){
    printf("USAGE: %s in-file.wav out-file.wav\n", str);
}
#endif

int main(int argc, char** argv) {
    char mark[] = "hola";
    WaveData mainVoice = {0, 0, NULL};
    WAVHeader header;
    unsigned short * where, *initialWhere;

    FILE * fdWAV;
    /* Loading the voice from the file */
    if (argc == 3) {
        if ((fdWAV = openWAV(argv[1], "rb", &header, 0)) == NULL) {
            return 0;
        }
#ifdef DEBUG
        printf("Input file opened\n");
#endif
    } else {
#ifdef DEBUG
        usage(argv[0]);
#endif
        return 1;
    }

    unsigned length = getLength(fdWAV);
#ifdef DEBUG
    printf("Length: %d \n", length);
#endif
    length = length / (MARK_SAMPLES * 2);

    FILE * outWAV = openWAV(argv[2], "w", &header, length * (MARK_SAMPLES * 2));
    if (outWAV == NULL) {
#ifdef DEBUG
    printf("Cant open output file: %s\n",argv[2]);
#endif
        return 1;
    }
#ifdef DEBUG
    printf("Output file opened\n");
#endif

    DWT_INIT();

    short* data = (short*) malloc(sizeof (short) *MARK_SAMPLES);
    if (!data)
        return 0;

    long i;
    unsigned len;
    char k = 7;
    for (i = 0; i < length; i++) {
        len = readWAV(fdWAV, MARK_SAMPLES * 2, (void*) data);

        if (i == 100 || i == 10 || i == 500) {
            putSynMark(data + MARK_SAMPLES - SYN_NSAMPLES);
        }
        if (i == 101 || i == 11 || i == 501) {
#ifdef DEBUG
            printf("hidden: %d\n", k);
#endif
            embedMethod(data, k);
            k *= 4;
        }
        len = saveWAV(outWAV, MARK_SAMPLES * 2, (void*) data);
    }

    if (data)
        free(data);

    closeWAV(&fdWAV);
    closeWAV(&outWAV);

#ifdef DEBUG
    printf("PROCESS ENDED OK!!!\n");
#endif
    return 0;
}
